package 剑指offer;

public class 剑指Offer58I翻转单词顺序 {
    public static void main(String[] args) {
        System.out.println(reverseWords("1 "));
    }

    public static String reverseWords(String s) {
        if("".equals(s)) return "";

        String[] str = s.split(" ");
        int i = 0;
        int j = str.length - 1;
        if(i == j) return str[i];
        while(i < j){
            String temp = str[i];
            str[i] = str[j];
            str[j] = temp;
            i++;
            j--;
        }
        StringBuilder sb = new StringBuilder();
        for(int k = 0;k < str.length;k++){
            if(str[k].equals("")){
                continue;
            }else{
                sb.append(str[k]);
                sb.append(" ");
            }
        }
        // sb.append(str[str.length - 1]);
        String s1 = sb.toString();
        if("".equals(s1)) return "";
        if(s1.substring(s1.length()-1,s1.length()).equals(" ")){
            return s1.substring(0,s1.length()-1);
        }
        return s1;
    }
}
